/**
 * 
 */
package com.archerdu.apply.service.impl;

import java.util.List;

import com.archerdu.apply.dao.OptionDAO;
import com.archerdu.apply.dao.QuestionDAO;
import com.archerdu.apply.model.Option;
import com.archerdu.apply.model.Question;
import com.archerdu.apply.service.OptionManager;
import com.archerdu.base.dao.HibernateBaseDao;
import com.archerdu.base.service.BaseManager;

/**
 * @author archer
 * @version 1.0.0
 */
public class OptionManagerImpl extends BaseManager<Option, Integer> implements
        OptionManager {

    private static final long serialVersionUID = 8596375322783640728L;

    private OptionDAO optionDao;
    private QuestionDAO questionDao;

    @Override
    public void delete(Option option) throws Exception {
        if(null == option || null == option.getId())
            throw new Exception("没有删除条件");
        option = optionDao.get(option.getId());
        
        if(null == option){
            throw new Exception("要删除的对象不存在");
        }
        super.delete(option);
    }

    /**
     * 添加或修改前检查对象
     * @param option
     * @throws Exception
     */
    private void checkOption(Option option) throws Exception{
        Question ques = questionDao.get( option.getQuestion().getId() );
        boolean doThrow = true;
        
        if(ques.getType().equals( QuestionDAO.TYPE_TEXT )){
        	// 填写题不需要选项
        	throw new Exception("该题目为填写，不需要备选答案");
        }
        if(option.getIsright().equals(OptionDAO.ISRIGHT_RIGHT  )){
        	Option opt = new Option();
        	opt.setQuestion( ques );
        	opt.setIsright( OptionDAO.ISRIGHT_RIGHT );
        	List<Option> list = optionDao.findWithQuestion( opt);
        	
        	if(option.getId() != null){
        		for(Option op1: list){
        			if(op1.getId().equals( op1.getId()) ){
        				doThrow = false;
        			}
        		}
        	}
        	if(doThrow){
        		// 单选检查是否唯一正确答案
		        if(ques.getType().equals( QuestionDAO.TYPE_RADIO) && list.size() > 0){
		        	throw new Exception("该题目为单选题，并已有正确答案");
		        // 多选检查正确答案数量是否超过备选答案的数量
		        }else if(ques.getType().equals( QuestionDAO.TYPE_CHECKBOX) 
		        		&& ques.getCount().equals( list.size() )){
		        	throw new Exception("该题目为多选题，正确答案数量已等于备选答案数量");
		        }
        	}
    	}
    }
    @Override
    public void add(Option entity) throws Exception {
        entity.setCreatetime(getSqlDate());
        checkOption(entity);
        super.add(entity);
    }
    
    @Override
    public void modify(Option entity) throws Exception {
        entity.setUpdatetime(getSqlDate());
        checkOption(entity);
        super.modify( entity );
    }

    public void setOptionDao(OptionDAO dao) {
        optionDao = dao;
    }

    public OptionDAO getOptionDao() {
        return optionDao;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected HibernateBaseDao<Option, Integer> getDao() {
        return (HibernateBaseDao<Option, Integer>) optionDao;
    }

	public QuestionDAO getQuestionDao() {

		return questionDao;
	}

	public void setQuestionDao( QuestionDAO questionDao ) {

		this.questionDao = questionDao;
	}
}
